/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.CoreGraphics;
using FlickrNet;
using System.Net;

namespace ArtNotes
{


	public class ANFullScreenFlickrImage : UIViewController
	{

		private UIScrollView imageScrollView;
		private UIImageView imageScrollViewImage;
		
		private UIImage Image;
		
		private Photo photo;
		
		private UIButton btnDismiss;
		
		private UIImage dismissButtonImage;
		
		public ANFullScreenFlickrImage (Photo photo)
		{
			this.View.BackgroundColor = UIColor.White;
			this.View.Frame = new RectangleF(0,0,320,480);
			this.photo = photo;
			
			dismissButtonImage = UIImage.FromFileUncached("Images/close_x.png");
		}
		
		public override void ViewWillAppear (bool animated)
		{
			this.Image = downloadImage(photo.MediumUrl, photo.PhotoId + ".jpg");
		}

		public UIImage downloadImage (string url, string filename)
		{
			String docdir = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
			WebClient wc = new WebClient ();
			wc.DownloadFile (url, docdir + "/" + filename);
			UIImage tmp = UIImage.FromFileUncached(docdir + "/" + filename);
			return tmp;
		}
		
		public override void ViewDidAppear (bool animated)
		{			
			imageScrollViewImage = new UIImageView(Image);
			imageScrollViewImage.Frame = new RectangleF (0,0,320,480);
			
			imageScrollView = new UIScrollView();
			
			imageScrollView.ContentSize = new SizeF(320f,480f);

			imageScrollView.MaximumZoomScale = 6.0f;
			imageScrollView.MinimumZoomScale = 0.5f;
			imageScrollView.ClipsToBounds = true;
			imageScrollView.Frame = new RectangleF (0,0,320,480);
			imageScrollView.BackgroundColor = UIColor.White;
			imageScrollView.AddSubview(imageScrollViewImage);
			
			imageScrollView.Delegate = new scrollViewDelegate(this);
			
			this.View.AddSubview(imageScrollView);
			
			//btnDismiss = RoundedButtonType(270,10,"X");
			
			btnDismiss = ImageButtonType(new RectangleF(270,10,55,58),dismissButtonImage,dismissButtonImage);
			this.View.AddSubview(btnDismiss);
			btnDismiss.TouchDown += delegate {
				this.DismissModalViewControllerAnimated(true);
			};
		}
		
		// From the monotouch samples
		private UIButton RoundedButtonType (float xpos, float ypos, string btitle)
		{
			var button = UIButton.FromType (UIButtonType.RoundedRect);
			button.Frame = new RectangleF (xpos, ypos, 40f, 40f);
			button.BackgroundColor = UIColor.Clear;
			button.SetTitle (btitle, UIControlState.Normal);
			button.SetTitleColor (UIColor.Black, UIControlState.Normal);

			button.Tag = 1;
			// To support reusable cells
			return button;
		}
		
		private UIButton ImageButtonType (RectangleF frame, UIImage defaultImage, UIImage pressedImage)
		{
			var background = defaultImage;//UIImage.FromFile("Images/btnSearch.png");
			var backgroundPressed = pressedImage;//UIImage.FromFile("Images/btnSearchPressed.png");
			//var frame = frame;
			
			var button = new UIButton(frame);
			var nbackground = background.StretchableImage(12,0);
			var nbackgroundPressed = backgroundPressed.StretchableImage(12,0);
			button.SetBackgroundImage(nbackground,UIControlState.Normal);
			button.SetBackgroundImage(nbackgroundPressed,UIControlState.Highlighted);

			button.Tag = 1;
			return button;
		}
		
		public class scrollViewDelegate : UIScrollViewDelegate
		{
			private ANFullScreenFlickrImage anFullScreenFlickrImage;
			
			public scrollViewDelegate(ANFullScreenFlickrImage anFullScreenFlickrImage)
			{
				this.anFullScreenFlickrImage = anFullScreenFlickrImage;
			}
			
			public override UIView ViewForZoomingInScrollView (UIScrollView scrollView)
			{
				return this.anFullScreenFlickrImage.imageScrollViewImage;
			}

		}
	}
}
